var Console = {
	messages: [],
	fullDisplay: false,
	
	addMessage: function(message, color, type){
		if (this.messages.length > 0){
			var lm = this.messages[this.messages.length - 1];
			if (lm.type == type){
				if (type == "itemStep"){
					lm.message = "You stepped on various items";
					return;
				}
				lm.repeat += 1;
				return;
			}
		}
		
		this.messages.push({
			message: message,
			color: color,
			repeat: 1,
			type: type
		});
	},
	
	toggleConsole: function(){
		this.fullDisplay = !this.fullDisplay;
	},
	
	drawConsole: function(ctx, playerBusy){
		var x1 = ctx.width / 2 - 300;
		var y1 = ctx.height - 16;
		var y2 = 16;
		var i1 = this.messages.length - 1;
		if (this.fullDisplay && !playerBusy){
			y1 = ctx.height - 128;
			y2 = 128;
			i1 = this.messages.length - 8;
		}
		y1 -= 4;
		y2 += 4;
		
		if (i1 < 0) i1 = 0;
		var i2 = i1 + 8;
		if (i2 > this.messages.length) i2 = this.messages.length;
		
		ctx.fillStyle = "rgba(0,30,0,0.8)";
		ctx.fillRect(x1, y1, 600, y2);
		ctx.strokeStyle = "white";
		ctx.strokeRect(x1, y1, 600, y2);
		
		ctx.textAlign = "left";
		
		y1 += 16;
		
		for (var i=i1;i<i2;i++){
			var msg = this.messages[i];
			var repeat = "";
			if (msg.repeat > 1) repeat = " (x" + msg.repeat + ")";
			
			ctx.fillStyle = msg.color;
			ctx.fillText(msg.message+repeat, x1+8, y1 + (16 * (i - i1)));
		}
	},
	
	getArticle: function(name){
		var lt = name[0].toLowerCase();
		if (lt == "a" || lt == "e" || lt == "i" || lt == "o" || lt == "u")
			return "an";
		return "a";
	}
};
